﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace DotNet.Common
{
    public class WriteLog
    {
        public delegate void LogAppendDelegate(Color color, string text);

        public RichTextBox richTextBoxRemote;
        public string timeFormatString = "HH:mm:ss "; //yyyy.MM.dd HH:mm:ss

        public WriteLog()
        {
        }

        public WriteLog(RichTextBox richTextBoxRemote)
        {
            this.richTextBoxRemote = richTextBoxRemote;
        }

        private void LogAppend(Color color, string text)
        {
            richTextBoxRemote.SelectionColor = color;
            richTextBoxRemote.AppendText(text);
            richTextBoxRemote.AppendText(Environment.NewLine);
        }

        public void LogAppendMsg(string text)
        {
            LogMessage(text, Color.Black, DateTime.Now.ToString(timeFormatString));
        }

        public void LogError(string text)
        {
            LogMessage(text, Color.Red, DateTime.Now.ToString(timeFormatString));
        }

        public void LogWarning(string text)
        {
            LogMessage(text, Color.Violet, DateTime.Now.ToString(timeFormatString));
        }

        public void LogMessage(string text)
        {
            LogMessage(text, Color.Black, DateTime.Now.ToString(timeFormatString));
        }

        public void LogMessage(string text, Color color)
        {
            LogMessage(text, color, DateTime.Now.ToString(timeFormatString));
        }

        public void LogMessage(string text, Color color, string pre)
        {
            LogAppendDelegate la = new LogAppendDelegate(LogAppend);
            richTextBoxRemote.Invoke(la, color, pre + text);
        }
    }
}